package cn.liweixi.org.controller;

import cn.liweixi.basic.utils.Result;
import cn.liweixi.basic.utils.PageList;
import cn.liweixi.org.domain.Employee;
import cn.liweixi.org.query.EmployeeQuery;
import cn.liweixi.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 介绍controller是哪块业务的
@Api(value = "员工的API",description="员工相关的功能")
@RequestMapping("/emp")
@RestController // Controller + responseBody 当前的controller返回的都是json
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //根据id查询数据
    @GetMapping("{id}")
    public Employee findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    //查询所有
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    //修改或添加
    @PutMapping
    public Result addOrUpdate(@RequestBody Employee employee) {
        try {
            if (null == employee.getId()){
                //添加数据
                employeeService.add(employee);
            }else{
                //修改数据
                employeeService.updateById(employee);
            }
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
            return Result.me().fail("加载异常了，再检查检查");
    }

    //删除数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    //分页查询
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }

    //批量删除
    @PatchMapping
    public Result batchDel(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }
}



